﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using HUGATE.ItemObject;
using CoreLibs;
using HUGATE.Utility;

namespace HUGATE.Item.Controls.Manufacturer
{
    /// <summary>
    /// Creator : thai huy
    /// Date    : 11/12/2010
    /// Desc    : Quản lý nhà sản xuất
    /// </summary>

    public partial class ManufacturerList : System.Web.UI.UserControl
    {

        #region Variables

        private int iPageSize = 10;
        private string strReturnPage = "";

        #endregion

        #region Properties

        public int PageSize
        {
            get { return iPageSize; }
            set { iPageSize = value; }
        }

        #endregion

        #region Permissions

       
        #endregion

        #region Support Functions

        /// <summary>
        /// Nạp danh sách dữ liệu vào lưới
        /// </summary>
        /// <param name="intPage"></param>
        private void GridDatabind(int intPage, string manufacturerID)
        {         
            ITEMManufacturer objManufacturer = new ITEMManufacturer();
            DataTable dtb = new DataTable();
            if (manufacturerID == "0")
            {
                dtb = objManufacturer.SearchData(txtSearch.Text.Trim());
            }
            else
            {
                dtb = objManufacturer.SearchData("");
            }
            grvObject.DataSource = dtb;
            grvObject.PageIndex = intPage;
            grvObject.DataBind();
            for (int i = 0; i < grvObject.Rows.Count; i++)
            {
                
                if (((Label)grvObject.Rows[i].FindControl("lblmanufacturer")).Text==manufacturerID.Trim())
                {
                    ((Image)grvObject.Rows[i].FindControl("imgnew")).Visible = true;
                    break;
                }
            }
        }       
        #endregion

        #region Event Functions

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtSearch.Attributes.Add("onblur", "CheckHTML('" + txtSearch.ClientID + "');");
                btnAddNew.Text = HUGATE.Utility.ResxManager.GetString("Btn_AddNew");
                btnReturn.Text = HUGATE.Utility.ResxManager.GetString("Btn_Return");
                btnSearch.Text = HUGATE.Utility.ResxManager.GetString("Btn_Search");

                grvObject.Columns[0].HeaderText = HUGATE.Utility.ResxManager.GetString("ID") + HUGATE.Utility.ResxManager.GetString("ITEMManufacturer");
                grvObject.Columns[1].HeaderText = HUGATE.Utility.ResxManager.GetString("Name") + HUGATE.Utility.ResxManager.GetString("ITEMManufacturer");
                grvObject.Columns[2].HeaderText = HUGATE.Utility.ResxManager.GetString("Description");
                grvObject.Columns[3].HeaderText = HUGATE.Utility.ResxManager.GetString("Default");
                grvObject.Columns[4].HeaderText = HUGATE.Utility.ResxManager.GetString("Actived");
                

                grvObject.EmptyDataText = string.Format(HUGATE.Utility.ResxManager.GetString("DetailItemNotFound"), HUGATE.Utility.ResxManager.GetString("ITEMManufacturer", true));

                GridDatabind(0,"0");
            }
            ctrlManufacturerEdit.SavedEvent += new ManufacturerEdit.SaveDelegate(ctrlManufacturerEdit_SavedEvent);
            btnReturn.OnClientClick = "window.location='" + Globals.ApplicationVRoot() + "/" + strReturnPage + "';return false;";
        }

        void ctrlManufacturerEdit_SavedEvent(object sender, EventArgs e, string manufactuerID)
        {
            ITEMManufacturer objManufacturer = new ITEMManufacturer();
            DataTable dtb = objManufacturer.SearchData("");
            int Rowsbg = dtb.Rows.Count;
            int page;
            page = Rowsbg / 20;
            int row = Rowsbg % 20;
            if (row == 0)
            {
                page += 1;
            }
            mpeUpdate.Hide();
            GridDatabind(page, manufactuerID);
            udpList.Update();
        }

      
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDatabind(0,"0");
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            ctrlManufacturerEdit.Clear();
            mpeUpdate.Show();
            udpEdit.Update();
            lblTitle.Text = HUGATE.Utility.ResxManager.GetString("PopupTitle_Insert");
        }
        protected void grvObject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridDatabind(e.NewPageIndex,"0");
        }
        protected void grvObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblTitle.Text = HUGATE.Utility.ResxManager.GetString("PopupTitle_Update");
            ctrlManufacturerEdit.ManufacturerID = Convert.ToInt32(grvObject.SelectedValue);
            ctrlManufacturerEdit.LoadInfo();
            mpeUpdate.Show();
            udpEdit.Update();
        }


        protected void imgDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbtTemp = sender as LinkButton;
            ITEMManufacturer objManufacturer = new ITEMManufacturer();
            objManufacturer.ManufacturerID = Convert.ToInt32(lbtTemp.CommandArgument);
            objManufacturer.CreatedUser = "administrator";

            if (objManufacturer.Delete())
            {
               GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("DeleteItemSuccessful"), HUGATE.Utility.ResxManager.GetString("ITEMManufacturer", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.Information);
                GridDatabind(grvObject.PageIndex,"0");
            }
           else
               GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("CouldNotDeleteParent"), HUGATE.Utility.ResxManager.GetString("ITEMManufacturer", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.WarningMessage);

        }
        #endregion
                
    }
}